package com.ro.common;

import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.activerecord.tx.TxByMethods;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import com.ro.common.model._MappingKit;
import com.ro.common.task.TaskService;
import com.ro.common.util.CodeUtil;
import com.ro.common.util.Util;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.logging.Logger;


public class RunConfig extends JFinalConfig {

    private Logger logger = Logger.getLogger(this.getClass().getName());
    private Timer timer = new Timer();

    private WallFilter wallFilter;

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 8082, "/");
    }

    /**
     * 配置常量
     */
    @Override
    public void configConstant(Constants me) {
        // 加载少量必要配置，随后可用PropKit.get(...)获取值
        PropKit.use("config.txt");
        me.setDevMode(PropKit.getBoolean("devMode", false));
        me.setError404View("/common/error_404.html");
        me.setError403View("/common/error_404.html");
        me.setError500View("/common/error_404.html");
    }

    /**
     * 配置路由
     */
    @Override
    public void configRoute(Routes me) {
        me.add(new RoutesConfig());
    }

    @Override
    public void configEngine(Engine me) {
        //模版
        me.addSharedFunction("/common/_layout.html");
        me.addSharedFunction("/common/_html.html");
        me.addSharedFunction("/common/_paginate.html");
        me.addSharedFunction("/common/_head.html");
//        me.addSharedFunction("/common/_right_plug.html");
        //方法
        me.addSharedMethod(new Util());
        //静态属性
        me.addSharedObject("WEB_SITE_NAME", PropKit.get("WEB_SITE_NAME"));
        me.addSharedObject("WEB_SITE_RECORD", PropKit.get("WEB_SITE_RECORD"));
        me.addSharedObject("WEB_SITE_AUTHOR", PropKit.get("WEB_SITE_AUTHOR"));
        me.addSharedObject("WEB_SITE_AUTHOR_AVATAR", PropKit.get("WEB_SITE_AUTHOR_AVATAR"));
        me.addSharedObject("IP_RECORD", PropKit.get("IP_RECORD"));
        me.addSharedObject("SAYING", PropKit.get("SAYING"));
        me.addSharedObject("TITLE", PropKit.get("TITLE"));
        me.addSharedObject("DESCR", PropKit.get("DESCR"));
        me.addSharedObject("KEYWORDS", PropKit.get("KEYWORDS"));
        me.addSharedObject("IM_QQ", PropKit.get("IM_QQ"));
        //热加载
        me.setDevMode(true);

    }

    /**
     * 配置插件
     */
    @Override
    public void configPlugin(Plugins me) {
        // 配置 druid 数据库连接池插件
        DruidPlugin druidPlugin = createDruidPlugin();
        wallFilter = new WallFilter();
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        me.add(druidPlugin);

        RedisPlugin ecRedis = new RedisPlugin("ec", PropKit.get("redisHost"),Integer.parseInt(PropKit.get("redisPort")));
        me.add(ecRedis);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        // 所有映射在 MappingKit 中自动化搞定
        _MappingKit.mapping(arp);
        me.add(arp);
    }

    public static DruidPlugin createDruidPlugin() {
        return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), CodeUtil.Decrypt(PropKit.get("password").trim(),PropKit.get("encryptionKey")));
    }

    /**
     * 配置全局拦截器
     */
    @Override
    public void configInterceptor(Interceptors me) {
        //事务拦截
        me.add(new TxByMethodRegex("(.*save.*|.*update.*|.*add.*|.*delete.*|.*create.*)"));
        me.add(new TxByMethods("save", "update", "add", "delete", "create"));
    }

    /**
     * 配置处理器
     */
    @Override
    public void configHandler(Handlers me) {

    }
}
